﻿using Microsoft.Practices.ServiceLocation ;
using OpenLightGroup.Prism ;
using System.ComponentModel.Composition ;
using System.Windows.Controls ;
using System.Windows.Media ;
using OpenLightGroup.Common;

namespace PrismExample
{
    [Export]
    public partial class Shell : UserControl
    {
        public Shell ( )
        {
            this.InitializeComponent( ) ;
            this.Loaded += new System.Windows.RoutedEventHandler( Shell_Loaded ) ;

        }

        [Import]
        public INetworkStatus NetworkStatus { get; set; }

        void Shell_Loaded ( object sender, System.Windows.RoutedEventArgs e )
        {
            try
            {
                var clogger = ServiceLocator.Current.GetInstance<ICompositeLogger>( ) ;
                
                this.DataContext = clogger.GetLogger<StringCollectionLogger>( ) ;                
            }
            catch (System.Exception)
            {

                throw ;
            }

            //We imported INetworkStatus, now we will listen for changes to the status.
            NetworkStatus.NetworkStatusChanged += new System.EventHandler( Current_NetworkStatusChanged ) ;

            this.UpdateBorder( ) ;
        }

        private void UpdateBorder ( )
        {
            if ( NetworkStatus.IsOnline )
            {
                this.RootBorder.BorderBrush = new SolidColorBrush( Colors.Green ) ;
            }
            else
                this.RootBorder.BorderBrush = new SolidColorBrush( Colors.Red ) ;
        }

        void Current_NetworkStatusChanged ( object sender, System.EventArgs e )
        {
            this.UpdateBorder( ) ;
        }

    }
}